using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using EM.Repository.utility;

namespace EM.admin.Menu
{
    public partial class Sections : System.Web.UI.Page
    {
        public int MENUID = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            int PageId = 31;
            Authentication Rua = new Authentication();
            Rua.CheckRolePageAuthentication(PageId, Authentication.RedirectionURLTypes.LoginPage);

            Int32.TryParse(Request.QueryString["ID"].ToString(), out MENUID);

            if (!IsPostBack)
            {
                rptSections_getdata();
            }
        }

        protected void rptSections_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            DbHelper srv = new DbHelper();
            SqlCommand cmd = new SqlCommand();

            if (e.CommandName == "Update")
            {
                DropDownList ddlContent = (DropDownList)rptSections.Items[e.Item.ItemIndex].FindControl("ddlContent");

                string sSQL = "";
                sSQL += " UPDATE TBLPAGESECTION SET ";
                sSQL += " CONTENTID=@CONTENTID ,";
                sSQL += " MODUSERID=@MODUSERID ,";
                sSQL += " MODDATE=GETDATE() ";
                sSQL += " WHERE PAGESECTIONID=@PAGESECTIONID ";

                cmd = new SqlCommand();
                cmd.CommandText = sSQL;
                if (ddlContent.SelectedValue == "0") { cmd.Parameters.Add("CONTENTID", SqlDbType.Int).Value = DBNull.Value; }
                else { cmd.Parameters.Add("CONTENTID", SqlDbType.Int).Value = ddlContent.SelectedValue; }
                cmd.Parameters.Add("PAGESECTIONID", SqlDbType.Int).Value = ((LinkButton)rptSections.Items[e.Item.ItemIndex].FindControl("btnUpdate")).CommandArgument;
                cmd.Parameters.Add("MODUSERID", SqlDbType.Int).Value = Session["USERID"].ToString();

                srv.ExecuteNonQuery(cmd);


                sSQL = " UPDATE TBLPAGESECTION_L SET ";
                sSQL += " SECTIONNAME = @SECTIONNAME ,";
                sSQL += " SHORTDESC = @SHORTDESC ,";
                sSQL += " MODUSERID=@MODUSERID ,";
                sSQL += " MODDATE=GETDATE() ";
                sSQL += " WHERE PAGESECTIONID=@PAGESECTIONID  ";
                sSQL += " AND LANGCODE = @LANGCODE "; 

                cmd = new SqlCommand();
                cmd.CommandText = sSQL;
                cmd.Parameters.Add("SECTIONNAME", SqlDbType.NVarChar).Value = ((TextBox)rptSections.Items[e.Item.ItemIndex].FindControl("txtName")).Text.Trim();
                cmd.Parameters.Add("SHORTDESC", SqlDbType.NVarChar).Value = ((TextBox)rptSections.Items[e.Item.ItemIndex].FindControl("txtShortDesc")).Text.Trim();
                cmd.Parameters.Add("PAGESECTIONID", SqlDbType.Int).Value = ((LinkButton)rptSections.Items[e.Item.ItemIndex].FindControl("btnUpdate")).CommandArgument;
                cmd.Parameters.Add("LANGCODE", SqlDbType.NVarChar).Value = ((HiddenField)rptSections.Items[e.Item.ItemIndex].FindControl("hfLANGCODE")).Value;
                cmd.Parameters.Add("MODUSERID", SqlDbType.Int).Value = Session["USERID"].ToString();

                srv.ExecuteNonQuery(cmd);

                rptSections_getdata();


            }

        }

        private void rptSections_getdata()
        {

            string sSQL = "";
            sSQL += " SELECT S.PAGESECTIONID , LANGCODE , SECTIONNAME , SHORTDESC,ISNULL(CONTENTID,0)CONTENTID ";
            sSQL += " FROM TBLPAGESECTION S , TBLPAGESECTION_L SL ";
            sSQL += " WHERE 1=1 ";
            sSQL += " AND S.PAGESECTIONID = SL.PAGESECTIONID ";
            sSQL += " AND PAGEID = @PAGEID ";
            sSQL += " AND LANGCODE = @LANGCODE";  
            
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = sSQL;
            cmd.Parameters.Add("PAGEID", SqlDbType.Int).Value = MENUID;
            cmd.Parameters.Add("LANGCODE", SqlDbType.NVarChar).Value = Session["LANGCODE"].ToString();
            
            DbHelper srv = new DbHelper();
            DataTable dt = srv.GetDataTable(cmd, "");
            if (dt.Rows.Count > 0)
            {
                rptSections.DataSource = dt;
                rptSections.DataBind();
            }
        }

        protected void rptSections_ItemdDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                //HiddenField hfLANGCODE = (HiddenField)e.Item.FindControl("hfLANGCODE");
                DropDownList ddlContent = (DropDownList)e.Item.FindControl("ddlContent");
                HiddenField hfContent = (HiddenField)e.Item.FindControl("hfContent");
                RequiredFieldValidator rfvName = (RequiredFieldValidator)e.Item.FindControl("rfvName");
                ValidationSummary vs = (ValidationSummary)e.Item.FindControl("vs");
                LinkButton btnUpdate = (LinkButton)e.Item.FindControl("btnUpdate");

                vs.ValidationGroup = rfvName.ValidationGroup = btnUpdate.ValidationGroup = "vg" + e.Item.ItemIndex;
                
                FillCombo fc = new FillCombo();
                fc.Content(ddlContent, hfContent.Value, true, Session["LANGCODE"].ToString());
                
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            rptSections_getdata();
        }
    }
}
